<!DOCTYPE html>
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html style="" class=" js no-touch svg inlinesvg svgclippaths no-ie8compat js no-touch svg inlinesvg svgclippaths no-ie8compat" lang="en"><!--<![endif]--><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <!-- Meta -->
        <meta charset="utf-8">

        <!-- Set the viewport width to device width for mobile -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <title>Zend\View Quick Start — Zend Framework 2 2.0.7 documentation - Zend Framework</title>

        <!-- CSS, feeds and favicon -->
        <link href="zend.view.quick-start_files/foundation.css" media="screen" rel="stylesheet" type="text/css">
        <link href="zend.view.quick-start_files/app.css" media="screen" rel="stylesheet" type="text/css">
        <link href="http://framework.zend.com/blog/feed-rss.xml" rel="alternate" type="application/rss+xml" title="Zend Framework Blog RSS Feed">
        <link href="http://framework.zend.com/blog/feed-atom.xml" rel="alternate" type="application/atom+xml" title="Zend Framework Blog Atom Feed">
        <link href="http://framework.zend.com/security/feed/" rel="alternate" type="application/rss+xml" title="Zend Framework Security Advisories">
        <link href="http://framework.zend.com/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">

        <!-- JavaScript -->
        <script type="text/javascript" src="zend.view.quick-start_files/modernizr.js"></script>
        
        <!-- IE Fix for HTML5 Tags -->
        <!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
    <script src="zend.view.quick-start_files/embed.js" async="" type="text/javascript"></script><script src="zend.view.quick-start_files/ga.js" async="" type="text/javascript"></script><script src="zend.view.quick-start_files/mTag.js" charset="iso-8859-1" type="text/javascript"></script><script charset="UTF-8" async="" src="zend.view.quick-start_files/alfie.js"></script></head>
    <body id="manual">

        <!-- Header -->
        <header id="header" class="row">

            <!-- Logo -->
            <div class="three mobile-two columns" id="logo">
                <h1>
                    <a href="http://framework.zend.com/" title="">
                        <img src="zend.view.quick-start_files/logo.png" alt="Zend Framework" height="41" width="173">
                    </a>
                </h1>
            </div>

            <!-- Menu -->
            <div class="six mobile-two columns" id="menu">
                <nav class="top-bar">
                    <ul>
                        <li class="name show-for-small">Menu</li>
                        <li class="toggle-topbar show-for-small"><a href="#"></a></li>
                        <li><a href="http://framework.zend.com/about/">About</a></li>
                        <li class="active"><a href="http://framework.zend.com/learn/">Learn</a></li>
                        <li><a href="http://framework.zend.com/downloads">Get Started</a></li>
                        <li><a href="http://framework.zend.com/participate/">Participate</a></li>
                        <li class="show-for-small">
                            <!-- Search form for small screens -->
                            <form method="get" action="//www.google.com/search" target="_blank">
                                <input name="q" maxlength="255" placeholder="Search in the site" type="text">
                                <input name="sitesearch" value="framework.zend.com" type="hidden">
                            </form>
                        </li>
                    </ul>
                </nav>
            </div>

            <!-- Search form -->
            <form method="get" action="//www.google.com/search" target="_blank" class="three columns hide-for-small">
                <input name="q" maxlength="255" placeholder="Search in the site" type="text">
                <input name="sitesearch" value="framework.zend.com" type="hidden">
            </form>

            <!-- Link to Github -->
                    </header>
        
        <div id="top-header-shadow"></div>

                    <div class="show-for-small" id="navigation-button">
                <a href="#navigation" class="secondary round button">…</a>
            </div>
            <div class="row">
                <!-- Navigation -->
                <nav id="navigation" class="three columns">
                    <div id="selectversion">
    Select a version:
    <select onchange="javascript:location.href='/manual/'+this.value+'/en/';">
        <option>2.1</option><option selected="selected">2.0</option><option>1.9</option><option>1.8</option><option>1.7</option><option>1.6</option><option>1.5</option><option>1.12</option><option>1.11</option><option>1.10</option><option>1.0</option>    </select>
</div>
<p></p>
  <h1><a href="http://framework.zend.com/manual/2.0/en/index.html">Table Of Contents</a></h1>
  <ul><li><a class="reference internal" href="#">Zend\View Quick Start</a></li>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#usage">Usage</a><ul><li><a class="reference internal" href="#configuration">Configuration</a></li>
<li><a class="reference internal" href="#controllers-and-view-models">Controllers and View Models</a></li>
<li><a class="reference internal" href="#nesting-view-models">Nesting View Models</a></li>
<li><a class="reference internal" href="#dealing-with-layouts">Dealing with Layouts</a></li>
<li><a class="reference internal" href="#creating-and-registering-alternate-rendering-and-response-strategies">Creating and Registering Alternate Rendering and Response Strategies</a></li>
</ul></li>
</ul><h1>Previous topic</h1>
  <p class="topless"><a href="http://framework.zend.com/manual/2.0/en/modules/zend.version.html" title="previous chapter">Getting the Zend Framework Version</a></p>
  <h1>Next topic</h1>
  <p class="topless"><a href="http://framework.zend.com/manual/2.0/en/modules/zend.view.renderer.php-renderer.html" title="next chapter">The PhpRenderer</a></p>
  <h1>This Page</h1>
  <ul class="this-page-menu"><li>
        <!--<a href="../_sources/modules/zend.view.quick-start.txt"-->
        <a href="https://github.com/zendframework/zf2-documentation/blob/master/docs/languages/en/modules/zend.view.quick-start.rst" rel="nofollow">Show Source</a></li>
    <li><a href="https://github.com/zendframework/zf2-documentation/edit/master/docs/languages/en/modules/zend.view.quick-start.rst" rel="nofollow">Edit Source</a>
    </li>
  </ul><p class="note">
            Note: You need to stay logged into your <a href="http://www.github.com/">GitHub account</a> to contribute to the documentation.
        </p>
<div id="searchbox" style="display: none">
  <h1>Quick search</h1>
    <form class="search" action="../search.html" method="get">
      <input name="q" type="text"><input value="Go" type="submit"><input name="check_keywords" value="yes" type="hidden"><input name="area" value="default" type="hidden"></form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript"><![CDATA[$('#searchbox').show(0);]]></script>                </nav>

                <!-- Content -->
                <div id="content" class="nine columns" role="main">
                    <div class="related hide-on-print"><ul><li class="prev"><a href="http://framework.zend.com/manual/2.0/en/modules/zend.version.html">Getting the Zend Framework Version</a></li><li class="next"><a href="http://framework.zend.com/manual/2.0/en/modules/zend.view.renderer.php-renderer.html">The PhpRenderer</a></li></ul></div><div class="body">
            
  <div class="section" id="zend-view-quick-start">
<span id="id1"></span><h1>Zend\View Quick Start<a class="headerlink" href="#zend-view-quick-start" title="Permalink to this headline">¶</a></h1>
</div>
<div class="section" id="overview">
<span id="zend-view-quick-start-intro"></span><h1>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h1>
<p><tt class="docutils literal"><span class="pre">Zend\View</span></tt> provides the “View” layer of Zend Framework 2’s MVC system. It is a multi-tiered system allowing a
variety of mechanisms for extension, substitution, and more.</p>
<p>The components of the view layer are as follows:</p>
<ul class="simple"><li><strong>Variables Containers</strong> hold variables and callbacks that you wish to represent in the view. Often-times,
a Variables Container will also provide mechanisms for context-specific escaping of variables and more.</li>
<li><strong>View Models</strong> hold Variables Containers, specify the template to use (if any), and optionally provide
rendering options (more on that below). View Models may be nested in order to represent complex structures.</li>
<li><strong>Renderers</strong> take View Models and provide a representation of them to return. Zend Framework 2 ships with three
renderers by default: a <tt class="docutils literal"><span class="pre">PhpRenderer</span></tt> which utilizes PHP templates in order to generate markup, a <tt class="docutils literal"><span class="pre">JsonRenderer</span></tt>,
and a <tt class="docutils literal"><span class="pre">FeedRenderer</span></tt> for generating RSS and Atom feeds.</li>
<li><strong>Resolvers</strong> utilizes Resolver Strategies to resolve a template name to a resource a Renderer may consume.
As an example, a Resolver may take the name “blog/entry” and resolve it to a PHP view script.</li>
<li>The <strong>View</strong> consists of strategies that map the current Request to a Renderer, and strategies for
injecting the result of rendering to the Response.</li>
<li><strong>Rendering Strategies</strong> listen to the “renderer” event of the View and decide which Renderer should be selected
based on the Request or other criteria.</li>
<li><strong>Response Strategies</strong> are used to inject the Response object with the results of rendering.
That may also include taking actions such as setting Content-Type headers.</li>
</ul><p>Additionally, Zend Framework 2 provides integration with the MVC via a number of event listeners in the
<tt class="docutils literal"><span class="pre">Zend\Mvc\View</span></tt> namespace.</p>
</div>
<div class="section" id="usage">
<span id="zend-view-quick-start-usage"></span><h1>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h1>
<p>This section of the manual is designed to show you typical usage patterns of the view layer when using it within
the Zend Framework 2 MVC. The assumptions are that you are using <a class="reference internal" href="http://framework.zend.com/manual/2.0/en/index.html#zend-di"><em>Dependency Injection</em></a> and the
default MVC view strategies.</p>
<div class="section" id="configuration">
<span id="zend-view-quick-start-usage-config"></span><h2>Configuration<a class="headerlink" href="#configuration" title="Permalink to this headline">¶</a></h2>
<p>The default configuration will typically work out-of-the-box. However, you will still need to
select Resolver Strategies and configure them, as well as potentially indicate alternate template names for things
like the site layout, 404 (not found) pages, and error pages. The code snippets below can be added to your
configuration to accomplish this. We recommend adding it to a site-specific module, such as the “Application”
module from the framework’s <a class="reference external" href="https://github.com/zendframework/ZendSkeletonApplication">ZendSkeletonApplication</a>, or to one of your autoloaded configurations within the
<tt class="docutils literal"><span class="pre">config/autoload/</span></tt> directory.</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">return</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">'view_manager'</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
        <span class="c1">// The TemplateMapResolver allows you to directly map template names</span>
        <span class="c1">// to specific templates. The following map would provide locations</span>
        <span class="c1">// for a home page template ("application/index/index"), as well as for</span>
        <span class="c1">// the layout ("layout/layout"), error pages ("error/index"), and</span>
        <span class="c1">// 404 page ("error/404"), resolving them to view scripts.</span>
        <span class="s1">'template_map'</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
            <span class="s1">'application/index/index'</span> <span class="o">=&gt;</span> <span class="nx">__DIR__</span> <span class="o">.</span>  <span class="s1">'/../view/application/index/index.phtml'</span><span class="p">,</span>
            <span class="s1">'site/layout'</span>           <span class="o">=&gt;</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">'/../view/layout/layout.phtml'</span><span class="p">,</span>
            <span class="s1">'error/index'</span>             <span class="o">=&gt;</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">'/../view/error/index.phtml'</span><span class="p">,</span>
            <span class="s1">'error/404'</span>               <span class="o">=&gt;</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">'/../view/error/404.phtml'</span><span class="p">,</span>
        <span class="p">),</span>

        <span class="c1">// The TemplatePathStack takes an array of directories. Directories</span>
        <span class="c1">// are then searched in LIFO order (it's a stack) for the requested</span>
        <span class="c1">// view script. This is a nice solution for rapid application</span>
        <span class="c1">// development, but potentially introduces performance expense in</span>
        <span class="c1">// production due to the number of static calls necessary.</span>
        <span class="c1">//</span>
        <span class="c1">// The following adds an entry pointing to the view directory</span>
        <span class="c1">// of the current module. Make sure your keys differ between modules</span>
        <span class="c1">// to ensure that they are not overwritten -- or simply omit the key!</span>
        <span class="s1">'template_path_stack'</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
            <span class="s1">'application'</span> <span class="o">=&gt;</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">'/../view'</span><span class="p">,</span>
        <span class="p">),</span>

        <span class="c1">// Set the template name for the site's layout.</span>
        <span class="c1">//</span>
        <span class="c1">// By default, the MVC's default Rendering Strategy uses the</span>
        <span class="c1">// template name "layout/layout" for the site's layout.</span>
        <span class="c1">// Here, we tell it to use the "site/layout" template,</span>
        <span class="c1">// which we mapped via the TemplateMapResolver above.</span>
        <span class="s1">'layout'</span> <span class="o">=&gt;</span> <span class="s1">'site/layout'</span><span class="p">,</span>

        <span class="c1">// By default, the MVC registers an "exception strategy", which is</span>
        <span class="c1">// triggered when a requested action raises an exception; it creates</span>
        <span class="c1">// a custom view model that wraps the exception, and selects a</span>
        <span class="c1">// template. We'll set it to "error/index".</span>
        <span class="c1">//</span>
        <span class="c1">// Additionally, we'll tell it that we want to display an exception</span>
        <span class="c1">// stack trace; you'll likely want to disable this by default.</span>
        <span class="s1">'display_exceptions'</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
        <span class="s1">'exception_template'</span> <span class="o">=&gt;</span> <span class="s1">'error/index'</span><span class="p">,</span>

       <span class="c1">// Another strategy the MVC registers by default is a "route not</span>
       <span class="c1">// found" strategy. Basically, this gets triggered if (a) no route</span>
       <span class="c1">// matches the current request, (b) the controller specified in the</span>
       <span class="c1">// route match cannot be found in the service locator, (c) the controller</span>
       <span class="c1">// specified in the route match does not implement the DispatchableInterface</span>
       <span class="c1">// interface, or (d) if a response from a controller sets the</span>
       <span class="c1">// response status to 404.</span>
       <span class="c1">//</span>
       <span class="c1">// The default template used in such situations is "error", just</span>
       <span class="c1">// like the exception strategy. Here, we tell it to use the "error/404"</span>
       <span class="c1">// template (which we mapped via the TemplateMapResolver, above).</span>
       <span class="c1">//</span>
       <span class="c1">// You can opt in to inject the reason for a 404 situation; see the</span>
       <span class="c1">// various `Application::ERROR_*`_ constants for a list of values.</span>
       <span class="c1">// Additionally, a number of 404 situations derive from exceptions</span>
       <span class="c1">// raised during routing or dispatching. You can opt-in to display</span>
       <span class="c1">// these.</span>
       <span class="s1">'display_not_found_reason'</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
       <span class="s1">'not_found_template'</span>       <span class="o">=&gt;</span> <span class="s1">'error/404'</span><span class="p">,</span>
    <span class="p">),</span>
<span class="p">);</span>
</pre></div>
</td></tr></tbody></table></div>
</div>
<div class="section" id="controllers-and-view-models">
<span id="zend-view-quick-start-usage-controllers"></span><h2>Controllers and View Models<a class="headerlink" href="#controllers-and-view-models" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">Zend\View\View</span></tt> consumes <a href="#id2"><span class="problematic" id="id3">``</span></a>ViewModel``s, passing them to the selected renderer. Where do you create these,
though?</p>
<p>The most explicit way is to create them in your controllers and return them.</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Foo\Controller</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Model\ViewModel</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">BazBatController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">doSomethingCrazyAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
            <span class="s1">'message'</span> <span class="o">=&gt;</span> <span class="s1">'Hello world'</span><span class="p">,</span>
        <span class="p">));</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'foo/baz-bat/do-something-crazy'</span><span class="p">);</span>
        <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>This sets a “message” variable in the View Model, and sets the template name “foo/baz-bat/do-something-crazy”.
The View Model is then returned.</p>
<p>In most cases, you’ll likely have a template name based on the module namespace, controller, and action.
Considering that, and if you’re simply passing some variables, could this be made simpler? Definitely.</p>
<p>The MVC registers a couple of listeners for controllers to automate this. The first will look to see if you
returned an associative array from your controller; if so, it will create a View Model and make this associative
array the Variables Container; this View Model then replaces the <a class="reference internal" href="http://framework.zend.com/manual/2.0/en/modules/zend.mvc.mvc-event.html#zend-mvc-mvc-event"><em>MvcEvent</em></a>‘s result.
It will also look to see if you returned nothing or null; if so, it will create a View Model without any variables
attached; this View Model also replaces the <tt class="docutils literal"><span class="pre">MvcEvent</span></tt>‘s result.</p>
<p>The second listener checks to see if the <tt class="docutils literal"><span class="pre">MvcEvent</span></tt> result is a View Model, and, if so, if it has a template
associated with it. If not, it will inspect the controller matched during routing to determine the module namespace
and the controller class name, and, if available, it’s “action” parameter in order to create a template name.
This will be “module/controller/action”, all normalized to lowercase, dash-separated words.</p>
<p>As an example, the controller <tt class="docutils literal"><span class="pre">Foo\Controller\BazBatController</span></tt> with action “doSomethingCrazyAction”, would be mapped
to the template <tt class="docutils literal"><span class="pre">foo/baz-bat/do-something-crazy</span></tt>. As you can see, the words “Controller” and “Action” are omitted.</p>
<p>In practice, that means our previous example could be re-written as follows:</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Foo\Controller</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">BazBatController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">doSomethingCrazyAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="k">array</span><span class="p">(</span>
            <span class="s1">'message'</span> <span class="o">=&gt;</span> <span class="s1">'Hello world'</span><span class="p">,</span>
        <span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>The above method will likely work for the majority of use cases. When you need to specify a different template,
explicitly create and return a View Model and specify the template manually, as in the first example.</p>
</div>
<div class="section" id="nesting-view-models">
<span id="zend-view-quick-start-usage-nesting"></span><h2>Nesting View Models<a class="headerlink" href="#nesting-view-models" title="Permalink to this headline">¶</a></h2>
<p>The other use case you may have for setting explicit View Models is if you wish to <strong>nest</strong> them.
In other words, you might want to render templates to be included within the main View you return.</p>
<p>As an example, you may want the View from an action to be one primary section that includes both an “article” and
a couple of sidebars; one of the sidebars may include content from multiple Views as well:</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Content\Controller</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Model\ViewModel</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">ArticleController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">viewAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// get the article from the persistence layer, etc...</span>

        <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">();</span>

        <span class="nv">$articleView</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'article'</span> <span class="o">=&gt;</span> <span class="nv">$article</span><span class="p">));</span>
        <span class="nv">$articleView</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'content/article'</span><span class="p">);</span>

        <span class="nv">$primarySidebarView</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">();</span>
        <span class="nv">$primarySidebarView</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'content/main-sidebar'</span><span class="p">);</span>

        <span class="nv">$secondarySidebarView</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">();</span>
        <span class="nv">$secondarySidebarView</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'content/secondary-sidebar'</span><span class="p">);</span>

        <span class="nv">$sidebarBlockView</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">();</span>
        <span class="nv">$sidebarBlockView</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'content/block'</span><span class="p">);</span>

        <span class="nv">$secondarySidebarView</span><span class="o">-&gt;</span><span class="na">addChild</span><span class="p">(</span><span class="nv">$sidebarBlockView</span><span class="p">,</span> <span class="s1">'block'</span><span class="p">);</span>

        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">addChild</span><span class="p">(</span><span class="nv">$articleView</span><span class="p">,</span> <span class="s1">'article'</span><span class="p">)</span>
             <span class="o">-&gt;</span><span class="na">addChild</span><span class="p">(</span><span class="nv">$primarySidebarView</span><span class="p">,</span> <span class="s1">'sidebar_primary'</span><span class="p">)</span>
             <span class="o">-&gt;</span><span class="na">addChild</span><span class="p">(</span><span class="nv">$secondarySidebarView</span><span class="p">,</span> <span class="s1">'sidebar_secondary'</span><span class="p">);</span>

        <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>The above will create and return a View Model specifying the template “content/article”. When the View is rendered,
it will render three child Views, the <tt class="docutils literal"><span class="pre">$articleView</span></tt>, <tt class="docutils literal"><span class="pre">$primarySidebarView</span></tt>, and <tt class="docutils literal"><span class="pre">$secondarySidebarView</span></tt>;
these will be captured to the <tt class="docutils literal"><span class="pre">$view</span></tt>‘s “article”, “sidebar_primary”, and “sidebar_secondary” variables,
respectively, so that when it renders, you may include that content. Additionally, the <tt class="docutils literal"><span class="pre">$secondarySidebarView</span></tt>
will include an additional View Model, <tt class="docutils literal"><span class="pre">$sidebarBlockView</span></tt>, which will be captured to its “block” view variable.</p>
<p>To better visualize this, let’s look at what the final content might look like, with comments detailing where each
nested view model is injected.</p>
<p>Here are the templates, rendered based on a 12-column grid:</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&lt;?</span><span class="nx">php</span> <span class="c1">// "content/article" template ?&gt;</span>
<span class="o">&lt;</span><span class="nx">div</span> <span class="nx">class</span><span class="o">=</span><span class="s2">"row content"</span><span class="o">&gt;</span>
    <span class="o">&lt;?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">article</span> <span class="cp">?&gt;</span><span class="x"></span>

<span class="x">    </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">sidebar_primary</span> <span class="cp">?&gt;</span><span class="x"></span>

<span class="x">    </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">sidebar_secondary</span> <span class="cp">?&gt;</span><span class="x"></span>
<span class="x">&lt;/div&gt;</span>

<span class="cp">&lt;?php</span> <span class="c1">// "content/article" template ?&gt;</span>
    <span class="o">&lt;!--</span> <span class="k">This</span> <span class="nx">is</span> <span class="nx">from</span> <span class="nx">the</span> <span class="nv">$articleView</span> <span class="nx">View</span> <span class="nx">Model</span><span class="p">,</span> <span class="k">and</span> <span class="nx">the</span> <span class="s2">"content/article"</span>
         <span class="nx">template</span> <span class="o">--&gt;</span>
    <span class="o">&lt;</span><span class="nx">article</span> <span class="nx">class</span><span class="o">=</span><span class="s2">"span8"</span><span class="o">&gt;</span>
        <span class="o">&lt;?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">escapeHtml</span><span class="p">(</span><span class="s1">'article'</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="x"></span>
<span class="x">    &lt;/article&gt;</span>

<span class="cp">&lt;?php</span> <span class="c1">// "content/main-sidebar" template ?&gt;</span>
    <span class="o">&lt;!--</span> <span class="k">This</span> <span class="nx">is</span> <span class="nx">from</span> <span class="nx">the</span> <span class="nv">$primarySidebarView</span> <span class="nx">View</span> <span class="nx">Model</span><span class="p">,</span> <span class="k">and</span> <span class="nx">the</span>
         <span class="s2">"content/main-sidebar"</span> <span class="nx">template</span> <span class="o">--&gt;</span>
    <span class="o">&lt;</span><span class="nx">div</span> <span class="nx">class</span><span class="o">=</span><span class="s2">"span2 sidebar"</span><span class="o">&gt;</span>
        <span class="nx">sidebar</span> <span class="nx">content</span><span class="o">...</span>
    <span class="o">&lt;/</span><span class="nx">div</span><span class="o">&gt;</span>

<span class="o">&lt;?</span><span class="nx">php</span> <span class="c1">// "content/secondary-sidebar template ?&gt;</span>
    <span class="o">&lt;!--</span> <span class="k">This</span> <span class="nx">is</span> <span class="nx">from</span> <span class="nx">the</span> <span class="nv">$secondarySidebarView</span> <span class="nx">View</span> <span class="nx">Model</span><span class="p">,</span> <span class="k">and</span> <span class="nx">the</span>
         <span class="s2">"content/secondary-sidebar"</span> <span class="nx">template</span> <span class="o">--&gt;</span>
    <span class="o">&lt;</span><span class="nx">div</span> <span class="nx">class</span><span class="o">=</span><span class="s2">"span2 sidebar pull-right"</span><span class="o">&gt;</span>
        <span class="o">&lt;?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">block</span> <span class="cp">?&gt;</span><span class="x"></span>
<span class="x">    &lt;/div&gt;</span>

<span class="cp">&lt;?php</span> <span class="c1">// "content/block template ?&gt;</span>
        <span class="o">&lt;!--</span> <span class="k">This</span> <span class="nx">is</span> <span class="nx">from</span> <span class="nx">the</span> <span class="nv">$sidebarBlockView</span> <span class="nx">View</span> <span class="nx">Model</span><span class="p">,</span> <span class="k">and</span> <span class="nx">the</span>
            <span class="s2">"content/block"</span> <span class="nx">template</span> <span class="o">--&gt;</span>
        <span class="o">&lt;</span><span class="nx">div</span> <span class="nx">class</span><span class="o">=</span><span class="s2">"block"</span><span class="o">&gt;</span>
            <span class="nx">block</span> <span class="nx">content</span><span class="o">...</span>
        <span class="o">&lt;/</span><span class="nx">div</span><span class="o">&gt;</span>
</pre></div>
</td></tr></tbody></table></div>
<p>And here is the aggregate, generated content:</p>
<div class="highlight-html"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24</pre></div></td><td class="code"><div class="highlight"><pre><span class="c">&lt;!-- This is from the $view View Model, and the "article/view" template --&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"row content"</span><span class="nt">&gt;</span>
    <span class="c">&lt;!-- This is from the $articleView View Model, and the "content/article"</span>
<span class="c">         template --&gt;</span>
    <span class="nt">&lt;article</span> <span class="na">class=</span><span class="s">"span8"</span><span class="nt">&gt;</span>
        Lorem ipsum ....
    <span class="nt">&lt;/article&gt;</span>

    <span class="c">&lt;!-- This is from the $primarySidebarView View Model, and the</span>
<span class="c">         "content/main-sidebar template --&gt;</span>
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"span2 sidebar"</span><span class="nt">&gt;</span>
        sidebar content...
    <span class="nt">&lt;/div&gt;</span>

    <span class="c">&lt;!-- This is from the $secondarySidebarView View Model, and the</span>
<span class="c">         "content/secondary-sidebar template --&gt;</span>
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"span2 sidebar pull-right"</span><span class="nt">&gt;</span>
        <span class="c">&lt;!-- This is from the $sidebarBlockView View Model, and the</span>
<span class="c">            "content/block template --&gt;</span>
        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"block"</span><span class="nt">&gt;</span>
            block content...
        <span class="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;/div&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>
</td></tr></tbody></table></div>
<p>As you can see, you can achieve very complex markup using nested Views, while simultaneously keeping the details of
rendering isolated from the Request/Response lifecycle of the controller.</p>
</div>
<div class="section" id="dealing-with-layouts">
<span id="zend-view-quick-start-usage-layouts"></span><h2>Dealing with Layouts<a class="headerlink" href="#dealing-with-layouts" title="Permalink to this headline">¶</a></h2>
<p>Most sites enforce a cohesive look-and-feel which we typically call the site’s “layout”. It includes the default
stylesheets and JavaScript necessary, if any, as well as the basic markup structure into which all site
content will be injected.</p>
<p>Within Zend Framework 2, layouts are handled via nesting of View Models (see the <a class="reference internal" href="#zend-view-quick-start-usage-nesting"><em>previous example</em></a> for examples of View Model nesting). The <tt class="docutils literal"><span class="pre">Zend\Mvc\View\Http\ViewManager</span></tt>
composes a View Model which acts as the “root” for nested View Models. As such, it should contain the skeleton
(or layout) template for the site. All other content is then rendered and captured to view variables of this root
View Model.</p>
<p>The <tt class="docutils literal"><span class="pre">ViewManager</span></tt> sets the layout template as “layout/layout” by default. To change this, you can add some
configuration to the “view_manager” area of your <a class="reference internal" href="#zend-view-quick-start-usage-config"><em>configuration</em></a>.</p>
<p>A listener on the controllers, <tt class="docutils literal"><span class="pre">Zend\Mvc\View\Http\InjectViewModelListener</span></tt>, will take a View Model returned from a
controller and inject it as a child of the root (layout) View Model. By default, View Models will capture to the
“content” variable of the root View Model. This means you can do the following in your layout view script:</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">&lt;</span><span class="nx">html</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="nx">head</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="nx">title</span><span class="o">&gt;&lt;?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">headTitle</span><span class="p">()</span> <span class="cp">?&gt;</span><span class="x">&lt;/title&gt;</span>
<span class="x">    &lt;/head&gt;</span>
<span class="x">    &lt;body&gt;</span>
<span class="x">        </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">content</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span>
<span class="x">    &lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span>
</pre></div>
</td></tr></tbody></table></div>
<p>If you want to specify a different View variable for which to capture, explicitly create a view model in your
controller, and set its “capture to” value:</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Foo\Controller</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Model\ViewModel</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">BazBatController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">doSomethingCrazyAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
            <span class="s1">'message'</span> <span class="o">=&gt;</span> <span class="s1">'Hello world'</span><span class="p">,</span>
        <span class="p">));</span>

        <span class="c1">// Capture to the layout view's "article" variable</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setCaptureTo</span><span class="p">(</span><span class="s1">'article'</span><span class="p">);</span>

        <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>There will be times you don’t want to render a layout. For example, you might be answering an API call which
expects JSON or an XML payload, or you might be answering an XHR request that expects a partial HTML payload. The
simplest way to do this is to explicitly create and return a view model from your controller, and mark it as
“terminal”, which will hint to the MVC listener that normally injects the returned View Model into the layout View
Model, to instead replace the layout view model.</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Foo\Controller</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Model\ViewModel</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">BazBatController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">doSomethingCrazyAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
            <span class="s1">'message'</span> <span class="o">=&gt;</span> <span class="s1">'Hello world'</span><span class="p">,</span>
        <span class="p">));</span>

        <span class="c1">// Disable layouts; `MvcEvent` will use this View Model instead</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setTerminal</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>

        <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p><a class="reference internal" href="#zend-view-quick-start-usage-nesting"><em>When discussing nesting View Models</em></a>, we detailed a nested
View Model which contained an article and sidebars. Sometimes, you may want to provide additional View Models to
the layout, instead of nesting in the returned layout. This may be done by using the “layout” controller plugin,
which returns the root View Model. You can then call the same <tt class="docutils literal"><span class="pre">addChild()</span></tt> method on it as we did in that
previous example.</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Content\Controller</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Model\ViewModel</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">ArticleController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">viewAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// get the article from the persistence layer, etc...</span>

        <span class="c1">// Get the "layout" view model and inject a sidebar</span>
        <span class="nv">$layout</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">layout</span><span class="p">();</span>
        <span class="nv">$sidebarView</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">();</span>
        <span class="nv">$sidebarView</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'content/sidebar'</span><span class="p">);</span>
        <span class="nv">$layout</span><span class="o">-&gt;</span><span class="na">addChild</span><span class="p">(</span><span class="nv">$sidebarView</span><span class="p">,</span> <span class="s1">'sidebar'</span><span class="p">);</span>

        <span class="c1">// Create and return a view model for the retrieved article</span>
        <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'article'</span> <span class="o">=&gt;</span> <span class="nv">$article</span><span class="p">));</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'content/article'</span><span class="p">);</span>
        <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>You could also use this technique to select a different layout, by simply calling the <tt class="docutils literal"><span class="pre">setTemplate()</span></tt> method of
the layout View Model:</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22</pre></div></td><td class="code"><div class="highlight"><pre><span class="c1">//In a controller</span>
<span class="k">namespace</span> <span class="nx">Content\Controller</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Model\ViewModel</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">ArticleController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">viewAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// get the article from the persistence layer, etc...</span>

        <span class="c1">// Get the "layout" view model and set an alternate template</span>
        <span class="nv">$layout</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">layout</span><span class="p">();</span>
        <span class="nv">$layout</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'article/layout'</span><span class="p">);</span>

        <span class="c1">// Create and return a view model for the retrieved article</span>
        <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'article'</span> <span class="o">=&gt;</span> <span class="nv">$article</span><span class="p">));</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'content/article'</span><span class="p">);</span>
        <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>Sometimes, you may want to access the layout from within your actual view scripts when using the <tt class="docutils literal"><span class="pre">PhpRenderer</span></tt>.
Reasons might include wanting to change the layout template or wanting to either access or inject layout view variables.
Similar to the “layout” controller plugin, you can use the “layout” View Helper. If you provide a string argument to it,
you will change the template; if you provide no arguments, the root layout View Model is returned.</p>
<p>Commonly, you may want to alter the layout based on the current <strong>module</strong>. This
requires (a) detecting if the controller matched in routing belongs to this module, and then (b) changing the
template of the View Model.</p>
<p>The place to do these actions is in a listener. It should listen either to the “route” event at low (negative)
priority, or on the “dispatch” event, at any priority. Typically, you will register this during the bootstrap
event.</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Content</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Module</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">onBootstrap</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// Register a dispatch event</span>
        <span class="nv">$app</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getParam</span><span class="p">(</span><span class="s1">'application'</span><span class="p">);</span>
        <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">getEventManager</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s1">'dispatch'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="s1">'setLayout'</span><span class="p">));</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">setLayout</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nv">$matches</span>    <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getRouteMatch</span><span class="p">();</span>
        <span class="nv">$controller</span> <span class="o">=</span> <span class="nv">$matches</span><span class="o">-&gt;</span><span class="na">getParam</span><span class="p">(</span><span class="s1">'controller'</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="k">false</span> <span class="o">!==</span> <span class="nb">strpos</span><span class="p">(</span><span class="nv">$controller</span><span class="p">,</span> <span class="nx">__NAMESPACE__</span><span class="p">))</span> <span class="p">{</span>
            <span class="c1">// not a controller from this module</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="c1">// Set the layout template</span>
        <span class="nv">$viewModel</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getViewModel</span><span class="p">();</span>
        <span class="nv">$viewModel</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">'content/layout'</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
</div>
<div class="section" id="creating-and-registering-alternate-rendering-and-response-strategies">
<span id="zend-view-quick-start-usage-strategies"></span><h2>Creating and Registering Alternate Rendering and Response Strategies<a class="headerlink" href="#creating-and-registering-alternate-rendering-and-response-strategies" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">Zend\View\View</span></tt> does very little. Its workflow is essentially to martial a <tt class="docutils literal"><span class="pre">ViewEvent</span></tt>, and then trigger two
events, “renderer” and “response”. You can attach “strategies” to these events, using the methods
<tt class="docutils literal"><span class="pre">addRenderingStrategy()</span></tt> and <tt class="docutils literal"><span class="pre">addResponseStrategy()</span></tt>, respectively. A Rendering Strategy investigates the
Request object (or any other criteria) in order to select a Renderer (or fail to select one). A Response Strategy
determines how to populate the Response based on the result of rendering.</p>
<p>Zend Framework 2 ships with three Rendering and Response Strategies that you can use within your application.</p>
<ul class="simple"><li><tt class="docutils literal"><span class="pre">Zend\View\Strategy\PhpRendererStrategy</span></tt>. This strategy is a “catch-all” in that it will always return the
<tt class="docutils literal"><span class="pre">Zend\View\Renderer\PhpRenderer</span></tt> and populate the Response body with the results of rendering.</li>
<li><tt class="docutils literal"><span class="pre">Zend\View\Strategy\JsonStrategy</span></tt>. This strategy inspects the Accept HTTP header, if present, and determines if
the client has indicated it accepts an “application/json” response. If so, it will return the
<tt class="docutils literal"><span class="pre">Zend\View\Renderer\JsonRenderer</span></tt>, and populate the Response body with the JSON value returned, as well as set
a Content-Type header with a value of “application/json”.</li>
<li><tt class="docutils literal"><span class="pre">Zend\View\Strategy\FeedStrategy</span></tt>. This strategy inspects the Accept HTTP header, if present, and determines if
the client has indicated it accepts either an “application/rss+xml” or “application/atom+xml” response. If so, it
will return the <tt class="docutils literal"><span class="pre">Zend\View\Renderer\FeedRenderer</span></tt>, setting the feed type to either “rss” or “atom”, based on
what was matched. Its Response strategy will populate the Response body with the generated feed, as well as set a
Content-Type header with the appropriate value based on feed type.</li>
</ul><p>By default, only the <tt class="docutils literal"><span class="pre">PhpRendererStrategy</span></tt> is registered, meaning you will need to register the other Strategies
yourself if you want to use them. Additionally, it means that you will likely want to register these at higher
priority to ensure they match before the <tt class="docutils literal"><span class="pre">PhpRendererStrategy</span></tt>. As an example, let’s register the <tt class="docutils literal"><span class="pre">JsonStrategy</span></tt>:</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Application</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Module</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">onBootstrap</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// Register a "render" event, at high priority (so it executes prior</span>
        <span class="c1">// to the view attempting to render)</span>
        <span class="nv">$app</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getApplication</span><span class="p">();</span>
        <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">getEventManager</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s1">'render'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="s1">'registerJsonStrategy'</span><span class="p">),</span> <span class="mi">100</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">registerJsonStrategy</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nv">$app</span>          <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getTarget</span><span class="p">();</span>
        <span class="nv">$locator</span>      <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">getServiceManager</span><span class="p">();</span>
        <span class="nv">$view</span>         <span class="o">=</span> <span class="nv">$locator</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">'Zend\View\View'</span><span class="p">);</span>
        <span class="nv">$jsonStrategy</span> <span class="o">=</span> <span class="nv">$locator</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">'ViewJsonStrategy'</span><span class="p">);</span>

        <span class="c1">// Attach strategy, which is a listener aggregate, at high priority</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">getEventManager</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="nv">$jsonStrategy</span><span class="p">,</span> <span class="mi">100</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>The above will register the <tt class="docutils literal"><span class="pre">JsonStrategy</span></tt> with the “render” event, such that it executes prior to the
<tt class="docutils literal"><span class="pre">PhpRendererStrategy</span></tt>, and thus ensure that a JSON payload is created when requested.</p>
<p>What if you want this to happen only in specific modules, or specific controllers? One way is similar to the last
example in the <a class="reference internal" href="#zend-view-quick-start-usage-layouts"><em>previous section on layouts</em></a>, where we detailed changing
the layout for a specific module:</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Content</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Module</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">onBootstrap</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// Register a render event</span>
        <span class="nv">$app</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getParam</span><span class="p">(</span><span class="s1">'application'</span><span class="p">);</span>
        <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">getEventManager</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s1">'render'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="s1">'registerJsonStrategy'</span><span class="p">),</span> <span class="mi">100</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">registerJsonStrategy</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nv">$matches</span>    <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getRouteMatch</span><span class="p">();</span>
        <span class="nv">$controller</span> <span class="o">=</span> <span class="nv">$matches</span><span class="o">-&gt;</span><span class="na">getParam</span><span class="p">(</span><span class="s1">'controller'</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="k">false</span> <span class="o">!==</span> <span class="nb">strpos</span><span class="p">(</span><span class="nv">$controller</span><span class="p">,</span> <span class="nx">__NAMESPACE__</span><span class="p">))</span> <span class="p">{</span>
            <span class="c1">// not a controller from this module</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="c1">// Potentially, you could be even more selective at this point, and test</span>
        <span class="c1">// for specific controller classes, and even specific actions or request</span>
        <span class="c1">// methods.</span>

        <span class="c1">// Set the JSON strategy when controllers from this module are selected</span>
        <span class="nv">$app</span>          <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getTarget</span><span class="p">();</span>
        <span class="nv">$locator</span>      <span class="o">=</span> <span class="nv">$app</span><span class="o">-&gt;</span><span class="na">getServiceManager</span><span class="p">();</span>
        <span class="nv">$view</span>         <span class="o">=</span> <span class="nv">$locator</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">'Zend\View\View'</span><span class="p">);</span>
        <span class="nv">$jsonStrategy</span> <span class="o">=</span> <span class="nv">$locator</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">'ViewJsonStrategy'</span><span class="p">);</span>

        <span class="c1">// Attach strategy, which is a listener aggregate, at high priority</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">getEventManager</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="nv">$jsonStrategy</span><span class="p">,</span> <span class="mi">100</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>While the above examples detail using the <tt class="docutils literal"><span class="pre">JsonStrategy</span></tt>, the same could be done for the <tt class="docutils literal"><span class="pre">FeedStrategy</span></tt>.</p>
<p>What if you want to use a custom renderer? Or if your app might allow a combination of JSON, Atom feeds, and HTML?
At this point, you’ll need to create your own custom strategies. Below is an example that appropriately loops
through the HTTP Accept header, and selects the appropriate Renderer based on what is matched first.</p>
<div class="highlight-php"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">namespace</span> <span class="nx">Content\View</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Zend\EventManager\EventCollection</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\EventManager\ListenerAggregateInterface</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Feed\Writer\Feed</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Renderer\FeedRenderer</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Renderer\JsonRenderer</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Renderer\PhpRenderer</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">AcceptStrategy</span> <span class="k">implements</span> <span class="nx">ListenerAggregateInterface</span>
<span class="p">{</span>
    <span class="k">protected</span> <span class="nv">$feedRenderer</span><span class="p">;</span>
    <span class="k">protected</span> <span class="nv">$jsonRenderer</span><span class="p">;</span>
    <span class="k">protected</span> <span class="nv">$listeners</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
    <span class="k">protected</span> <span class="nv">$phpRenderer</span><span class="p">;</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">(</span>
        <span class="nx">PhpRenderer</span> <span class="nv">$phpRenderer</span><span class="p">,</span>
        <span class="nx">JsonRenderer</span> <span class="nv">$jsonRenderer</span><span class="p">,</span>
        <span class="nx">FeedRenderer</span> <span class="nv">$feedRenderer</span>
    <span class="p">)</span> <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">phpRenderer</span>  <span class="o">=</span> <span class="nv">$phpRenderer</span><span class="p">;</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">jsonRenderer</span> <span class="o">=</span> <span class="nv">$jsonRenderer</span><span class="p">;</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">feedRenderer</span> <span class="o">=</span> <span class="nv">$feedRenderer</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">attach</span><span class="p">(</span><span class="nx">EventCollection</span> <span class="nv">$events</span><span class="p">,</span> <span class="nv">$priority</span> <span class="o">=</span> <span class="k">null</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="k">null</span> <span class="o">===</span> <span class="nv">$priority</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">listeners</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$events</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s1">'renderer'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="s1">'selectRenderer'</span><span class="p">));</span>
            <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">listeners</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$events</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s1">'response'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="s1">'injectResponse'</span><span class="p">));</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">listeners</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$events</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s1">'renderer'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="s1">'selectRenderer'</span><span class="p">),</span> <span class="nv">$priority</span><span class="p">);</span>
            <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">listeners</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$events</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s1">'response'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="s1">'injectResponse'</span><span class="p">),</span> <span class="nv">$priority</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">detach</span><span class="p">(</span><span class="nx">EventCollection</span> <span class="nv">$events</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">foreach</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">listeners</span> <span class="k">as</span> <span class="nv">$index</span> <span class="o">=&gt;</span> <span class="nv">$listener</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="nv">$events</span><span class="o">-&gt;</span><span class="na">detach</span><span class="p">(</span><span class="nv">$listener</span><span class="p">))</span> <span class="p">{</span>
                <span class="nb">unset</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">listeners</span><span class="p">[</span><span class="nv">$index</span><span class="p">]);</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">selectRenderer</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getRequest</span><span class="p">();</span>
        <span class="nv">$headers</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">getHeaders</span><span class="p">();</span>

        <span class="c1">// No Accept header? return PhpRenderer</span>
        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$headers</span><span class="o">-&gt;</span><span class="na">has</span><span class="p">(</span><span class="s1">'accept'</span><span class="p">))</span> <span class="p">{</span>
            <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">phpRenderer</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="nv">$accept</span> <span class="o">=</span> <span class="nv">$headers</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">'accept'</span><span class="p">);</span>
        <span class="k">foreach</span> <span class="p">(</span><span class="nv">$accept</span><span class="o">-&gt;</span><span class="na">getPrioritized</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$mediaType</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="mi">0</span> <span class="o">===</span> <span class="nb">strpos</span><span class="p">(</span><span class="nv">$mediaType</span><span class="p">,</span> <span class="s1">'application/json'</span><span class="p">))</span> <span class="p">{</span>
                <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">jsonRenderer</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="mi">0</span> <span class="o">===</span> <span class="nb">strpos</span><span class="p">(</span><span class="nv">$mediaType</span><span class="p">,</span> <span class="s1">'application/rss+xml'</span><span class="p">))</span> <span class="p">{</span>
                <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">feedRenderer</span><span class="o">-&gt;</span><span class="na">setFeedType</span><span class="p">(</span><span class="s1">'rss'</span><span class="p">);</span>
                <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">feedRenderer</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="mi">0</span> <span class="o">===</span> <span class="nb">strpos</span><span class="p">(</span><span class="nv">$mediaType</span><span class="p">,</span> <span class="s1">'application/atom+xml'</span><span class="p">))</span> <span class="p">{</span>
                <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">feedRenderer</span><span class="o">-&gt;</span><span class="na">setFeedType</span><span class="p">(</span><span class="s1">'atom'</span><span class="p">);</span>
                <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">feedRenderer</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="c1">// Nothing matched; return PhpRenderer. Technically, we should probably</span>
        <span class="c1">// return an HTTP 415 Unsupported response.</span>
        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">phpRenderer</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">injectResponse</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nv">$renderer</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getRenderer</span><span class="p">();</span>
        <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getResponse</span><span class="p">();</span>
        <span class="nv">$result</span>   <span class="o">=</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getResult</span><span class="p">();</span>

        <span class="k">if</span> <span class="p">(</span><span class="nv">$renderer</span> <span class="o">===</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">jsonRenderer</span><span class="p">)</span> <span class="p">{</span>
            <span class="c1">// JSON Renderer; set content-type header</span>
            <span class="nv">$headers</span> <span class="o">=</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getHeaders</span><span class="p">();</span>
            <span class="nv">$headers</span><span class="o">-&gt;</span><span class="na">addHeaderLine</span><span class="p">(</span><span class="s1">'content-type'</span><span class="p">,</span> <span class="s1">'application/json'</span><span class="p">);</span>
        <span class="p">}</span> <span class="k">elseif</span> <span class="p">(</span><span class="nv">$renderer</span> <span class="o">===</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">feedRenderer</span><span class="p">)</span> <span class="p">{</span>
            <span class="c1">// Feed Renderer; set content-type header, and export the feed if</span>
            <span class="c1">// necessary</span>
            <span class="nv">$feedType</span>  <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">feedRenderer</span><span class="o">-&gt;</span><span class="na">getFeedType</span><span class="p">();</span>
            <span class="nv">$headers</span>   <span class="o">=</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getHeaders</span><span class="p">();</span>
            <span class="nv">$mediatype</span> <span class="o">=</span> <span class="s1">'application/'</span>
                       <span class="o">.</span> <span class="p">((</span><span class="s1">'rss'</span> <span class="o">==</span> <span class="nv">$feedType</span><span class="p">)</span> <span class="o">?</span> <span class="s1">'rss'</span> <span class="o">:</span> <span class="s1">'atom'</span><span class="p">)</span>
                       <span class="o">.</span> <span class="s1">'+xml'</span><span class="p">;</span>
            <span class="nv">$headers</span><span class="o">-&gt;</span><span class="na">addHeaderLine</span><span class="p">(</span><span class="s1">'content-type'</span><span class="p">,</span> <span class="nv">$mediatype</span><span class="p">);</span>

            <span class="c1">// If the $result is a feed, export it</span>
            <span class="k">if</span> <span class="p">(</span><span class="nv">$result</span> <span class="nx">instanceof</span> <span class="nx">Feed</span><span class="p">)</span> <span class="p">{</span>
                <span class="nv">$result</span> <span class="o">=</span> <span class="nv">$result</span><span class="o">-&gt;</span><span class="na">export</span><span class="p">(</span><span class="nv">$feedType</span><span class="p">);</span>
            <span class="p">}</span>
        <span class="p">}</span> <span class="k">elseif</span> <span class="p">(</span><span class="nv">$renderer</span> <span class="o">!==</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">phpRenderer</span><span class="p">)</span> <span class="p">{</span>
            <span class="c1">// Not a renderer we support, therefor not our strategy. Return</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="c1">// Inject the content</span>
        <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">setContent</span><span class="p">(</span><span class="nv">$result</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td></tr></tbody></table></div>
<p>This strategy would be registered just as we demonstrated registering the <tt class="docutils literal"><span class="pre">JsonStrategy</span></tt> earlier. You would also
need to define DI configuration to ensure the various renderers are injected when you retrieve the strategy from
the application’s locator instance.</p>
</div>
</div>


          </div><div class="related hide-on-print"><ul><li class="prev"><a href="http://framework.zend.com/manual/2.0/en/modules/zend.version.html">Getting the Zend Framework Version</a></li><li class="next"><a href="http://framework.zend.com/manual/2.0/en/modules/zend.view.renderer.php-renderer.html">The PhpRenderer</a></li></ul></div>    <div id="disqus_thread"><iframe verticalscrolling="no" horizontalscrolling="no" src="zend.view.quick-start_files/a.html" style="width: 100%; border: medium none; overflow: hidden; height: 1657px;" role="complementary" allowtransparency="true" data-disqus-uid="1" id="dsq1" frameborder="0" scrolling="no" width="100%"></iframe><iframe src="zend.view.quick-start_files/client.html" style="width: 100%; border: medium none; overflow: hidden; display: none;" role="application" allowtransparency="true" data-disqus-uid="3" id="dsq3" frameborder="0" width="100%"></iframe><iframe style="width: 893px; border: medium none; overflow: hidden; top: 0px; min-width: 893px; max-width: 893px; position: fixed; max-height: 29px; min-height: 29px; height: 29px; display: none;" role="alert" allowtransparency="true" data-disqus-uid="-indicator-north" id="dsq-indicator-north" frameborder="0" scrolling="no"></iframe><iframe style="width: 893px; border: medium none; overflow: hidden; bottom: 0px; min-width: 893px; max-width: 893px; position: fixed; max-height: 29px; min-height: 29px; height: 29px; display: none;" role="alert" allowtransparency="true" data-disqus-uid="-indicator-south" id="dsq-indicator-south" frameborder="0" scrolling="no"></iframe></div>
    <script type="text/javascript">
        var disqus_developer = 0;
        var disqus_shortname = 'zfmanual';
        var disqus_identifier = 'zfmanual-modules/zend.view.quick-start.html';
        var disqus_url = 'http://framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html';
        var disqus_title = 'Zend\View Quick Start — Zend Framework 2 2.0.7 documentation';

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
                    </div>
            </div>
        
        <!-- Footer -->
        <footer id="footer">
            <div class="row">
                <div class="two mobile-two columns">
                    <h4>About</h4>
                    <ul>
                        <li class="link-highlight"><a href="http://framework.zend.com/about/">Overview</a></li>
                        <li><a href="http://framework.zend.com/about/faq/">ZF2 FAQ</a></li>
                        <li><a href="http://framework.zend.com/about/faq-v1/">ZF1 FAQ</a></li>

                                                <li><a href="http://framework.zend.com/security/">Security</a></li>
                        <li><a href="http://framework.zend.com/changelog/">Changelog</a></li>
                        <li><a href="http://framework.zend.com/blog/index.html">Blog</a></li>
                    </ul>
                </div>

                <div class="two mobile-two columns">
                    <h4>Learn</h4>
                    <ul>
                        <li><a href="http://framework.zend.com/manual/2.1/en/user-guide/overview.html">User Guide</a></li>
                        <li><a href="http://framework.zend.com/manual/2.1/en/index.html">Reference Guide</a></li>
                        <li><a href="http://framework.zend.com/docs/api/">APIs</a></li>
                        <!--              <li><a href="">Webinars</a></li>-->
                        <li><a href="http://framework.zend.com/learn/training-and-certification/">Training &amp; Certification</a></li>
                        <li><a href="http://framework.zend.com/learn/support-and-consulting/">Support &amp; Consulting</a></li>
                    </ul>
                </div>

                <hr>

                <div class="two mobile-two columns">
                    <h4>Get Started</h4>
                    <ul>
                        <li><a href="http://framework.zend.com/downloads">Downloads</a></li>
                        <li><a href="http://framework.zend.com/downloads/skeleton-app">Get the Skeleton App</a></li>
                        <li><a href="http://framework.zend.com/downloads/phpcloud">Try on <span class="link-highlight">phpcloud.com</span></a></li>
                    </ul>
                </div>

                <div class="two mobile-two columns">
                    <h4>Participate</h4>
                    <ul>
                        <li><a href="http://framework.zend.com/participate/">Overview</a></li>
                        <li><a href="http://framework.zend.com/participate/contributor-guide/">Contributors Guide</a></li>
                                                <li><a href="http://framework.zend.com/participate/blogs/">Blogs</a></li>
                                            </ul>
                </div>

                <div class="two mobile-four columns offset-by-two" id="contact-menu">
                    <h4>Contact Us</h4>
                    <ul>
                        <li><a href="http://framework.zend.com/contact">form</a></li>
                        <li><a href="http://framework.zend.com/archives/">mailing lists</a></li>
                        <li><a href="http://framework.zend.com/irc/">IRC</a></li>
                    </ul>
                </div>
            </div>

            <div class="row">
                <ul id="social-links" class="block-grid four-up mobile-two-up">
                    <li>
                        <a href="https://github.com/zendframework/zf2">
                            <i class="foundicon-github"></i>
                        </a>
                    </li>
                    <li>
                        <a href="https://twitter.com/zfdevteam">
                            <i class="foundicon-twitter"></i>
                        </a>
                    </li>
                    <li>
                        <a href="http://framework.zend.com/blog/feed-rss.xml">
                            <i class="foundicon-rss blog"></i>
                        </a>
                    </li>
                    <li>
                        <a href="http://framework.zend.com/security/feed/">
                            <i class="foundicon-rss security"></i>
                        </a>
                    </li>
                </ul>
            </div>

            <div id="footer-copyright" class="row">
                <p>© 2006 - 2013 by Zend Technologies Ltd. All rights reserved.</p>
                <a href="#header" id="to_top">
                    <img src="zend.view.quick-start_files/arrow.png" alt="" title="back to top" height="20" width="22">
                </a>
            </div>
        </footer>

        <!-- Included JS Files -->
        <script type="text/javascript" src="zend.view.quick-start_files/jquery.js"></script>
        <script type="text/javascript" src="zend.view.quick-start_files/foundation.js"></script>
        <script type="text/javascript" src="zend.view.quick-start_files/app.js"></script>

        <!-- Initialize JS Plugins -->
        <script>
            $(document).foundationNavigation();
        </script>
        
        <!-- Google Analytics -->
        <script type="text/javascript">
            function setCustomVar() {
                var t = _gat._getTrackerByName();
                if (t._getVisitorCustomVar(1) === undefined) {
                    t._setCustomVar(1, 'subdomains', '<value:framework>', 2);
                }
            }
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-291885-1']);
            _gaq.push(['_setDomainName', '.zend.com']);
            _gaq.push(setCustomVar);
            _gaq.push(['_trackPageview']);

            (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
            })();

            var google_conversion_id = 1072138399;
            var google_conversion_label = "PTX5CIfdwAMQn5Ge_wM";
            var google_custom_params = window.google_tag_params;
            var google_remarketing_only = true;
        </script>

        <!-- Eloqua integration -->
        <script type="text/javascript" src="zend.view.quick-start_files/elqCfg.js"></script>
        <script type="text/javascript" src="zend.view.quick-start_files/elqImg.js"></script><layer hidden="true"><img src="zend.view.quick-start_files/svrGP.gif" border="0" height="1" width="1"></layer>
        <script type="text/javascript" src="zend.view.quick-start_files/conversion.js"></script><iframe name="google_conversion_frame" src="zend.view.quick-start_files/a_002.html" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" frameborder="0" height="13" scrolling="no" width="300">&lt;img
 height="1" width="1" border="0" 
src="http://googleads.g.doubleclick.net/pagead/viewthroughconversion/1072138399/?frame=0&amp;random=1366167883842&amp;cv=7&amp;fst=1366167883842&amp;num=1&amp;fmt=1&amp;label=PTX5CIfdwAMQn5Ge_wM&amp;guid=ON&amp;u_h=768&amp;u_w=1360&amp;u_ah=744&amp;u_aw=1295&amp;u_cd=24&amp;u_his=4&amp;u_tz=420&amp;u_java=false&amp;u_nplug=6&amp;u_nmime=44&amp;ref=http%3A//www.google.com/url%3Fsa%3Dt%26rct%3Dj%26q%3D%26esrc%3Ds%26source%3Dweb%26cd%3D1%26ved%3D0CDAQFjAA%26url%3Dhttp%253A%252F%252Fframework.zend.com%252Fmanual%252F2.0%252Fen%252Fmodules%252Fzend.view.quick-start.html%26ei%3DRhFuUb-qIJOjiQeBu4CgDg%26usg%3DAFQjCNGb8SFHsNVA75paZ6KYFz3noce3wA%26sig2%3DJsym1E9UXpKSS-&amp;url=http%3A//framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html&amp;frm=0"
 /&gt;</iframe>
        <noscript>
            <div style="display: inline;">
                <img height="1" width="1" style="border-style:none;" alt="" src="http://googleads.g.doubleclick.net/pagead/viewthroughconversion/1072138399/?value=0&amp;label=PTX5CIfdwAMQn5Ge_wM&amp;guid=ON&amp;script=0">
            </div>
        </noscript>

        <!-- LivePerson integration -->
        <script type="text/javascript">
            var lpMTagConfig = {'lpServer' : "server.iad.liveperson.net",'lpNumber' : "10919998",'lpProtocol' : "https"}; function lpAddMonitorTag(src){if(typeof(src)=='undefined'||typeof(src)=='object'){src=lpMTagConfig.lpMTagSrc?lpMTagConfig.lpMTagSrc:'/hcp/html/mTag.js';}if(src.indexOf('http')!=0){src=lpMTagConfig.lpProtocol+"://"+lpMTagConfig.lpServer+src+'?site='+lpMTagConfig.lpNumber;}else{if(src.indexOf('site=')<0){if(src.indexOf('?')<0)src=src+'?';else src=src+'&';src=src+'site='+lpMTagConfig.lpNumber;}};var s=document.createElement('script');s.setAttribute('type','text/javascript');s.setAttribute('charset','iso-8859-1');s.setAttribute('src',src);document.getElementsByTagName('head').item(0).appendChild(s);} if (window.attachEvent) window.attachEvent('onload',lpAddMonitorTag); else window.addEventListener("load",lpAddMonitorTag,false);
        </script>
    

</body></html>